layui.define(['table', 'form'], function(exports){
  var $ = layui.$,table = layui.table,form = layui.form;

  table.render({
    elem: '#LAY-table'
    ,url: layui.setter.webRoot + '/market/goods/list.data'
    ,method: 'post'
    ,parseData: function (res) {
        return {
            "code": res.code,
            "msg": res.msg,
            "count": res.body.total,
            "data": res.body.data
        };
    }
    ,response: {statusCode: 200}
      ,size:'lg'
    ,cols: [[
      {type: 'checkbox', fixed: 'left'}
      ,{field: 'id', title: 'ID'}
      ,{field: 'code', title: '云商品ID', minWidth: 100}
      ,{field: 'name', title: '商品名称', minWidth: 100}
      ,{field: 'thumbnailImg', title: '商品图片', width: 100, templet: '#imgTpl'}
      ,{field: 'typeNames', title: '商品类型',templet:function (record){
          return  record.typeNames + '【'+record.classifyName+'】';
      }}
      ,{field: 'payMoneyStr', title: '零售价'}
      ,{field: 'stateName',title: '状态', width: 70,templet:function (record){
          if(record.auditState == 2){
              return record.auditStateName + '('+record.auditId+')'
          }else if(record.auditState == 3) {
              if (record.state == 1) {
                  return '<span class="layui-badge layui-bg-green">' + record.stateName + '</span>';
              } else if (record.state == 2) {
                  return '<span class="layui-badge layui-bg-orange">' + record.stateName + '</span>';
              } else if (record.auditState == 3) {
                  return '<span class="layui-badge">' + record.stateName + '</span>';
              } else {
                  return record.stateName;
              }
          }else {
              return record.auditStateName;
          }
      }}
      ,{field: 'createDate', title: '创建时间', sort: true}
      ,{title: '操作', width: 200, align:'center', fixed: 'right',templet: function (record) {
          var html = '<a class="layui-btn layui-btn-xs" lay-event="info">详情</a>';
          if(record.auditState == 3){
              if(record.state == 1) {
                  html = html + '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="downGoods">下架</a>';
              }else{
                  html = html + '<a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="upGoods">上架</a>';
              }
          }
          if(record.auditState == 2 || record.auditState == 3){
              if(record.state != 1) {
                  html = html + '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="cancelAudit">取消发布</a>';
              }
          }else {
              html = '<a class="layui-btn layui-btn-xs" lay-event="update">修改</a>';
			  if(record.auditState == 1) {
				html = html + '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="toAudit">发布</a>';
			  }
              html = html + '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
          }
          return html;
      }}
    ]]
    ,page: true
    ,limit: 30
    ,height: 'full-220'
    ,text: '对不起，加载出现异常！'
  });
  
  //监听搜索
  form.on('submit(LAY-search)', function(data){
    var field = data.field;
    //执行重载
    table.reload('LAY-table', {
      where: field
    });
  });
  
  //事件
  var active = {
    add: function(){
      layer.open({
        type: 2
        ,title: '新增商品'
        ,content: layui.setter.webRoot + '/market/goods/add.html'
        ,maxmin: true
        ,area: ['90%', '90%']
        ,btn: ['确定', '取消']
        ,yes: function(index, layero){
          var iframeWindow = window['layui-layer-iframe'+ index]
          ,submitID = 'LAY-form-submit'
          ,submit = layero.find('iframe').contents().find('#'+ submitID);

          //监听提交
          iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
            var field = data.field; //获取提交的字段
        	$.ajax({
	    		type: "post",
	            url: layui.setter.webRoot + '/market/goods/add.do',
	            data:field,
				dataType:"json",
				success:function(res){
					if(res.code == 200){
						table.reload('LAY-table'); //数据刷新
						layer.close(index); //关闭弹层
					}else{
						layer.msg(res.msg,{icon: 2});
					}
				}
	        });
          });  
          submit.trigger('click');
        }
      }); 
    }
    ,update:function(obj){
    	layer.open({
            type: 2
            ,title: '编辑商品'
            ,content: layui.setter.webRoot + '/market/goods/update.html?id='+obj.data.id
            ,maxmin: true
            ,area: ['90%', '90%']
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
              var iframeWindow = window['layui-layer-iframe'+ index]
              ,submitID = 'LAY-form-submit'
              ,submit = layero.find('iframe').contents().find('#'+ submitID);

              iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                var field = data.field; //获取提交的字段
                $.ajax({
    	    		type: "post",
    	            url: layui.setter.webRoot + '/market/goods/update.do',
    	            data:field,
    				dataType:"json",
    				success:function(res){
    					if(res.code == 200){
    						table.reload('LAY-table'); //数据刷新
    						layer.close(index); //关闭弹层
    					}else{
    						layer.msg(res.msg,{icon: 2});
    					}
    				}
    	        });
              });  
              
              submit.trigger('click');
            }
          });
    }
    ,info:function(obj){
          layer.open({
              type: 2
              ,title: '商品详情'
              ,content: layui.setter.webRoot + '/market/goods/info.html?id='+obj.data.id
              ,maxmin: true
              ,area: ['90%', '90%']
              ,btn: ['取消']
          });
    }
    ,del:function(obj){
    	layer.confirm('确定要删除该商品吗？', function(index){
	    	$.ajax({
	      	  type: "post",
	          url: layui.setter.webRoot + '/market/goods/del.do',
	          data:{id:obj.data.id},
	  		  dataType:"json",
	  		  success:function(res){
	  			if(res.code == 200){
	  				obj.del();
	  			}else{
	  				layer.msg(res.msg,{icon: 5});
	  			}
	  		  }
	        });
	    	layer.close(index);
	    });
    }
    ,toAudit:function (obj){
        layer.confirm('确定要发布到云商品广场吗？', function(index){
            var loadIndex = layer.load();
            $.ajax({
                  type: "post",
                  url: layui.setter.webRoot + '/market/goods/audit/apply.do',
                  data:{id:obj.data.id},
                  dataType:"json",
                  success:function(res){
                      layer.close(loadIndex);
                      if(res.code == 200){
                          table.reload('LAY-table'); //数据刷新
                      }else{
                          layer.msg(res.msg,{icon: 5});
                      }
                  }
            });
            layer.close(index);
        });
     }
     ,cancelAudit:function(obj){
          layer.confirm('确定要取消发布的商品吗？', function(index){
              $.ajax({
                  type: "post",
                  url: layui.setter.webRoot + '/market/goods/audit/cancel.do',
                  data:{id:obj.data.id},
                  dataType:"json",
                  success:function(res){
                      if(res.code == 200){
                          table.reload('LAY-table');
                      }else{
                          layer.msg(res.msg,{icon: 5});
                      }
                  }
              });
              layer.close(index);
          });
     }
    ,upGoods:function(obj){
          layer.confirm('确定要上架该商品吗？', function(index){
              $.ajax({
                  type: "post",
                  url: layui.setter.webRoot + '/market/goods/up.do',
                  data:{id:obj.data.id},
                  dataType:"json",
                  success:function(res){
                      if(res.code == 200){
                          table.reload('LAY-table');
                      }else{
                          layer.msg(res.msg,{icon: 5});
                      }
                  }
              });
              layer.close(index);
          });
      }
      ,downGoods:function(obj){
          layer.confirm('确定要下架该商品吗？', function(index){
              $.ajax({
                  type: "post",
                  url: layui.setter.webRoot + '/market/goods/down.do',
                  data:{id:obj.data.id},
                  dataType:"json",
                  success:function(res){
                      if(res.code == 200){
                          table.reload('LAY-table'); //数据刷新
                      }else{
                          layer.msg(res.msg,{icon: 5});
                      }
                  }
              });
              layer.close(index);
          });
      }
  };
  
  $('.layui-btn.layui-btn-fun').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
  table.on('tool(LAY-table)', function(obj){
  	var type = obj.event ;
   	active[type] ? active[type].call(this,obj) : '';
  });
  
  exports('goods_list', {})
});